# -*- coding: utf-8 -*-
from MyApi import *
import thosttraderapi as api
import os
import time



class CTradeSpi(api.CThostFtdcTraderSpi):
	myapi=''
	def __init__(self,myapi):
		api.CThostFtdcTraderSpi.__init__(self)
		self.myapi=myapi
		
	def OnFrontConnected(self) -> "void":
		print("receive OnFrontConnected")
		self.myapi.ctlevent.set()

	def OnFrontDisconnected(self, nReason: 'int') -> "void":
		print("receive OnFrontDisconnected, Reason:", nReason)

	def OnRspAuthenticate(self, pRspAuthenticateField: 'CThostFtdcRspAuthenticateField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":	
		PrintReName("OnRspAuthenticate")
		if(pRspInfo.ErrorID == 0):
			PrintReMsg(pRspInfo)
			self.myapi.ctlevent.set()
		else:
			PrintReMsg(pRspInfo)
			os._exit(233)
		
	def OnRspUserLogin(self, pRspUserLogin: 'CThostFtdcRspUserLoginField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
		PrintReName("OnRspUserLogin")
		if(pRspInfo.ErrorID == 0):
			PrintReMsg(pRspInfo)
			self.myapi.sessionid = pRspUserLogin.SessionID
			self.myapi.frontid = pRspUserLogin.FrontID
			self.myapi.ctlevent.set()
		else:
			PrintReMsg(pRspInfo)
			os._exit(233)

	def OnRspSettlementInfoConfirm(self, pSettlementInfoConfirm: 'CThostFtdcSettlementInfoConfirmField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
		PrintReName("OnRspSettlementInfoConfirm")
		if(pRspInfo.ErrorID == 0):
			PrintReMsg(pRspInfo)
			self.myapi.ctlevent.set()
		else:
			PrintReMsg(pRspInfo)
			os._exit(233)

	def OnRspOrderInsert(self, pInputOrder: 'CThostFtdcInputOrderField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
		if(self.myapi.orderpathconfirm != True):
			PrintReName("OnRspOrderInsert")
			if(pRspInfo.ErrorID == 0):
				PrintReMsg(pRspInfo)
				self.myapi.orderpathconfirm = True
			else:
				PrintReMsg(pRspInfo)
				os._exit(233)



		
	def OnRtnOrder(self, pOrder: 'CThostFtdcOrderField' ) -> "void":
		end_time = time.time()
		PrintReName("OnRtnOrder")
		start_time = self.myapi.timecalc[self.myapi.ORDERREF][0]
		if(pOrder.OrderRef == self.myapi.ORDERREF and pOrder.SessionID == self.myapi.sessionid and pOrder.FrontID == self.myapi.frontid):
			print ("investorid=", pOrder.InvestorID , "orderstatus=", pOrder.OrderStatus, "OrderRef=", pOrder.OrderRef)
			if(pOrder.OrderStatus == "a"):
				self.myapi.timecalc[self.myapi.ORDERREF][1] = end_time
				self.myapi.timecalc[self.myapi.ORDERREF][3] = (end_time - start_time) * 1000
				print('used time:', round(self.myapi.timecalc[self.myapi.ORDERREF][3], 3), 'ms')
			else:
				self.myapi.timecalc[self.myapi.ORDERREF][2] = end_time
				self.myapi.timecalc[self.myapi.ORDERREF][4] = (end_time - start_time) * 1000
				print('used time:', round(self.myapi.timecalc[self.myapi.ORDERREF][4], 3), 'ms')
				self.myapi.ctlevent.set()


#static function
#deal with too max float
def DealTooMax(mdlist):
	maxprice = 1e+15
	minprice = -1e+15
	for i in range(len(mdlist)):
		if((type(mdlist[i]) is not str) and (mdlist[i] > maxprice or mdlist[i] < minprice)):
			mdlist[i] = 0
		elif(type(mdlist[i]) is float):
			mdlist[i] = round(mdlist[i], 6)
		elif(mdlist[i] is None or mdlist[i] == ""):
			mdlist[i] = "NULL"
		else:
			mdlist[i] = mdlist[i]

#print errormsg
def PrintReMsg(pRspInfo):
	if pRspInfo is not None:
		remsg = "ErrorID= " + str(pRspInfo.ErrorID) + " ErrorMsg= " + str(pRspInfo.ErrorMsg)
		print(remsg)
	else:
		print("ErrorID= null ErrorMsg= null")
		return 1

#print func name
def PrintReName(rename):
	remsg = "receive" + rename
	print(remsg)
